package com.apedad.quartz.worker;

import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @author RocLiu [rocliu@tcl.com]
 * @version 1.0
 */
@Component
public class SampleWorker {

    @Autowired
    private JobDetail sampleJob;
    @Autowired
    private CronTrigger sampleTrigger;
    @Autowired
    private Scheduler sampleScheduler;

    @Scheduled(fixedDelay = 2 * 1000)
    public void work() throws SchedulerException {
        CronTrigger trigger = (CronTrigger) sampleScheduler.getTrigger(sampleTrigger.getKey());
        String currentExpression = trigger.getCronExpression();
        System.out.println(currentExpression);

        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/1 * * * * ? *");
        trigger = (CronTrigger) sampleScheduler.getTrigger(sampleTrigger.getKey());

        trigger.getTriggerBuilder().withSchedule(scheduleBuilder).build();

        sampleScheduler.rescheduleJob(sampleTrigger.getKey(), trigger);
        if (!sampleScheduler.isStarted()) {
            sampleScheduler.start();
        }

    }
}
